package week05.q10;

import java.sql.*;

/**
 * @author lv
 * @date 2021/4/17 21:29
 */
public class Test1 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionPool.getConnection();
        String sql = "insert into user(name,password) values(?,?)";
        PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
        pst.setString(1,"xiaoming");
        pst.setString(2, "123");
        pst.executeUpdate();
        ResultSet rs1 = pst.getGeneratedKeys();

        int id1 = 1;
        while (rs1.next()){
            id1  = rs1.getInt(1);
            System.out.println(id1);
        }

        sql = "update user set name = 'lv' where id = " + id1;
        pst.executeUpdate(sql);

        sql = " select * from user order by id limit 1";
        int id2 = 1;
        ResultSet rs2 = pst.executeQuery(sql);
        while (rs2.next()){
            id2 = rs2.getInt("id");
            System.out.println(id2);
        }
        sql = " delete from user where id = " + id2;
        pst.executeUpdate(sql);

        //关闭资源
        ConnectionPool.close(conn);
        conn.close();

    }
}
